package com.nine.shop.controller.admin;

import com.nine.shop.common.ServerResponse;
import com.nine.shop.controller.BaseController;
import com.nine.shop.model.Admin;
import com.nine.shop.service.AdminService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController

@RequestMapping(value = "api/shopping/admin",method = RequestMethod.POST)
public class AdminController extends BaseController {

    @Resource
    private AdminService adminService;


    /**
     * 管理员登录
     * @param
     * @param
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "login.do")
    public ServerResponse login( Admin admin){
       // admin.setAdminName("aa");
       // admin.setPassword("1234");
        ServerResponse message = adminService.login(admin.getAdminName(),admin.getPassword());
        if (message.getData() != null){
            super.getSession().setAttribute("admininfo",message.getData());
        }
        return message;
    }

    /**
     * 修改管理员密码
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "updatepassword.do")
    public ServerResponse updatePassword (String oldPassword,String newPassword ) {
           Admin  admin = (Admin) super.getSession().getAttribute("admininfo");
          int id = admin.getId();
        return (ServerResponse)adminService.updatePassword(id,oldPassword,newPassword);
    }

}
